// components/slide-item/slide-item.js
const app = getApp();
Page({
  data: {
    touchStartPageX: 0,
    scrollLeft: [],
    items: [],
    numbers: [],
    users: [],
    isShow: false,
    shows: [],
    nowPage: 1,
    pageLength: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: "浏览记录",
    })
    that.loadLogs();
  },
  
  loadLogs: function(){
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    wx.request({
      url: app.globalData.targetUrl + 'history/item/',
      method: "GET",
      data: {
        user_id: user_id,
        is_publish: 0,
        now_page: that.data.nowPage,
        page_cnt: 5
      },
      success: function (res) {
        var items = res.data.data;
        if (items.length === 0) {
          that.setData({
            isShow: false
          })
        } else {
          that.setData({
            items: items,
            isShow: true,
            pageLength: items[0].page_length,
          })
        }
      }
    })
  },

  //点击跳转到详情页面
  onDetail:function(e){
    var item_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/detail/detail?itemId=' + item_id
    });
  },

  touchStart: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      touchStartPageX: e.changedTouches[0].pageX,
    })
  },

  touchEnd: function (e) {
    var index = e.currentTarget.dataset.index;
    let touchEndPageX = e.changedTouches[0].pageX;
    var left = "scrollLeft[" + index + "]";
    var offSetStartToEnd = touchEndPageX - this.data.touchStartPageX;
    if (offSetStartToEnd < 10 & offSetStartToEnd > -10) {
      return;
    };
    if (offSetStartToEnd > 10) {
      if (this.data.scrollLeft === 0) return;
      this.setData({
        [left]: 0
      });
    };
    if (offSetStartToEnd < -50) {
      this.setData({
        [left]: 60
      })
    }
  },
  deleteTouchEnd: function (e) {
    var index = e.currentTarget.dataset.index;
    var that = this;
    var item_id = e.currentTarget.dataset.item_id;
    var user_id = wx.getStorageSync('user_id');
    console.log(user_id);
    console.log(item_id);
    var left = "scrollLeft[" + index + "]";
    that.setData({
      [left]: 0
    });
    wx.request({
      url: app.globalData.targetUrl + 'history/item/',
      method: "DELETE",
      data: {
        'user_id': user_id,
        'item_id': item_id,
        'is_publish': 0
      },
      success: function (res) {
        that.loadLogs();
      }
    })
  },
  deleteAll: function(){
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    console.log(user_id);
    wx.request({
      url: app.globalData.targetUrl + 'history/item/',
      method: "delete",
      data: {
        'user_id': user_id,
        'is_publish': 0
      },
      success: function (res) {
        that.loadLogs();
      }
    })
  },
  onClickButton1: function () {
    var that = this;
    var page = that.data.nowPage - 1;
    if (page >= 1) {
      that.setData({
        nowPage: page,
        items: [],
        numbers: [],
        users: [],
        shows: []
      })
      that.loadLogs();
    }
  },
  onClickButton2: function (e) {
    var that = this;
    var page = that.data.nowPage + 1;
    var pageLength = that.data.pageLength;
    if (page <= pageLength) {
      that.setData({
        nowPage: page,
        items: [],
        numbers: [],
        users: [],
        shows: []
      })
      that.loadLogs();
    }
  },
})